/**
 * Prueba 3
 * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package coba.service.impl;

import java.util.List;
import java.util.Map;

import com.liferay.portal.kernel.dao.orm.Criterion;
import com.liferay.portal.kernel.dao.orm.DynamicQuery;
import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
import com.liferay.portal.kernel.dao.orm.OrderFactoryUtil;
import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
import com.liferay.portal.kernel.dao.orm.RestrictionsFactoryUtil;
import com.liferay.portal.kernel.exception.SystemException;

import coba.model.Institucion;
import coba.service.base.InstitucionLocalServiceBaseImpl;
import coba.service.persistence.InstitucionUtil;

/**
 * The implementation of the Institucion local service.
 *
 * <p>
 * All custom service methods should be put in this class. Whenever methods are added, rerun ServiceBuilder to copy their definitions into the {@link coba.service.InstitucionLocalService} interface.
 *
 * <p>
 * This is a local service. Methods of this service will not have security checks based on the propagated JAAS credentials because this service can only be accessed from within the same VM.
 * </p>
 *
 * @author rodri042
 * @see coba.service.base.InstitucionLocalServiceBaseImpl
 * @see coba.service.InstitucionLocalServiceUtil
 */
public class InstitucionLocalServiceImpl extends InstitucionLocalServiceBaseImpl {
	/*
	 * NOTE FOR DEVELOPERS:
	 *
	 * Never reference this interface directly. Always use {@link coba.service.InstitucionLocalServiceUtil} to access the Institucion local service.
	 */
	
	private DynamicQuery createDynamicQueryFromMap(Map<String, String> map, boolean count) {
		DynamicQuery query = DynamicQueryFactoryUtil.forClass(Institucion.class);

		if (map.get("provinciaId") != null && !map.get("provinciaId").trim().equals(""))
			query.add(PropertyFactoryUtil.forName("provinciaId").eq(Long.valueOf(map.get("provinciaId").trim())));
		
		if (map.get("filtro") != null && !map.get("filtro").trim().equals("")) {
			String valor = "%" + map.get("filtro").trim() + "%";
			Criterion criterio = RestrictionsFactoryUtil.like("nombre", valor);
			criterio = RestrictionsFactoryUtil.or(criterio, RestrictionsFactoryUtil.like("localidad", valor));
			query.add(criterio);
		}

		query.addOrder(OrderFactoryUtil.asc("nombre"));

		return query;
	}

	public List<Institucion> findByMap(Map<String, String> map, int start, int end) throws SystemException {
		DynamicQuery query = createDynamicQueryFromMap(map, false);

		return InstitucionUtil.findWithDynamicQuery(query, start, end);
	}

	public long countByMap(Map<String, String> map) throws SystemException {
		DynamicQuery query = createDynamicQueryFromMap(map, true);

		return InstitucionUtil.countWithDynamicQuery(query);
	}
}
